package com.elitel.frame.base.service.impl;

import java.util.List;

import com.elitel.common.exception.BaseException;
import com.elitel.common.utils.UUIDUtil;
import com.elitel.frame.main.entity.dto.ServerQueryTypeAddDTO;
import com.elitel.frame.main.entity.dto.ServerQueryTypeDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.elitel.frame.base.service.BaseServicesService;
import com.elitel.frame.core.service.impl.BaseServiceImpl;
import com.elitel.frame.main.dao.BaseServicesMapper;
import com.elitel.frame.main.dao.ext.ExtBaseServicesMapper;
import com.elitel.frame.main.entity.BaseServices;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author gyf
 * @Description 服务基本信息服务实现
 */
@Service
public class BaseServicesServiceImpl extends BaseServiceImpl
        implements BaseServicesService {
    @Autowired
    private ExtBaseServicesMapper extBaseServicesMapper;
    @Autowired
    private BaseServicesMapper baseServicesMapper;

    @Override
    public List<BaseServices> getAllServices(String notIncludeGuid) {
        return extBaseServicesMapper.getAllList(notIncludeGuid);
    }

    @Override
    public List<BaseServices> getAllServices() {
        return extBaseServicesMapper.getAllList(null);
    }

    @Override
    public BaseServices getServerByGuid(String guid) {
        BaseServices service = baseServicesMapper.selectByPrimaryKey(guid);
        return service;
    }

    @Override
    public BaseServices getBaseServiceByGuid(String guid) {
        BaseServices baseservice = baseServicesMapper.selectByPrimaryKey(guid);
        return baseservice;
    }

    @Override
    public List<BaseServices> getServerQueryType(ServerQueryTypeDTO queryTypeDTO) {
        return extBaseServicesMapper.getServerQueryType(queryTypeDTO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveServerQueryType(ServerQueryTypeAddDTO serverQueryTypeAddDTO) {
        this.checkCodeExist(serverQueryTypeAddDTO.getGuid(), serverQueryTypeAddDTO.getServicecode());
        serverQueryTypeAddDTO.setGuid(UUIDUtil.randomUUID());
        extBaseServicesMapper.saveServerQueryType(serverQueryTypeAddDTO);
    }

    @Override
    public void updateServerQueryType(ServerQueryTypeAddDTO serverQueryTypeAddDTO) {
        this.checkCodeExist(serverQueryTypeAddDTO.getGuid(), serverQueryTypeAddDTO.getServicecode());
        extBaseServicesMapper.updateServerQueryType(serverQueryTypeAddDTO);
    }

    private void checkCodeExist(String guid, String servicecode) {
        List<BaseServices> list = this.extBaseServicesMapper.selectNeGuidEqCode(guid, servicecode);
        if (list != null && !list.isEmpty()) {
            throw new RuntimeException("服务编码已存在, 请重新填写! ");
        }
    }

    @Override
    public void delServerQueryType(ServerQueryTypeAddDTO serverQueryTypeAddDTO) {
        extBaseServicesMapper.delServerQueryType(serverQueryTypeAddDTO.getGuid());
    }

}
